#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

void gotoxy(short x, short y) 
{
	COORD pos = {x, y};
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void loading()
{
	int i;
	for (i=15;i<66;i++)
		{
			gotoxy(i,15);printf ("%c",196);
			gotoxy(i,17);printf ("%c",196);
		}
	gotoxy(14,16);printf ("%c",219);
	gotoxy(66,16);printf ("%c",219);
	gotoxy(36,16);printf ("LOADING");
	for (i=15;i<66;i++)
		{
			if ((i>=36)&&(i<43)) continue;
			gotoxy(i,16);printf ("%c",219);
			gotoxy(61,18);printf ("%3.d%%",(i-15)*2);
			Sleep (100);
		}
	gotoxy(1,23);
	Sleep(2000);
}
void huongdan()
{
	system("cls");
	printf("\n\t\t\tSu dung phim %c de di chuyen len\n",24);
	printf("\t\t\tSu dung phim %c de di chuyen xuong\n",25);
	printf("\t\t\tSu dung phim %c de di chuyen sang trai\n",26);
	printf("\t\t\tSu dung phim %c de di chuyen sang phai\n",27);
	Sleep(10000);
}
void gameover()
{
	system("cls");
	printf("   000       00     00     00  000000       0000   00   00  00000  00000\n");
	printf(" 00         00 0    000   000  00         00    0  00   00  00     00   0\n");
	printf(" 00  000   00   0   00 0 0 00  000000     00    0  00   00  00000  00 00  \n");
	printf(" 00   0   00000000  00  0  00  00         00    0   00 00   00     00  00 \n");
	printf("   000    00     0  00     00  000000       0000      0     00000  00   00 \n");
	getch();
}
void star(char &ch, bool check[100][100])
{
	system("cls");
	gotoxy(0,0);
	printf("                00000   00     0     00      00  00   00000\n");
	printf("               00       000    0    00 0     00 00    00   \n");
	printf("                 00     00 00  0   00   0    000      00000\n");
	printf("                   00   00  00 0  0000 000   00 00    00   \n");
	printf("               00000    00   000  00    00   00  00   00000\n");
	gotoxy(35,7);
	printf("1.Co dien");
	gotoxy(35,8);
	printf("2.Tu do");
	gotoxy(35,9);
	printf("3.Thoat");
	gotoxy(35,10);
	printf("4.Huong dan");
	while (!(ch=='1' || ch=='2' || ch=='3'|| ch=='4')) ch=getch();
	
	if (ch!='3' && ch !='4')
	{
	system("cls");
	for (int i=0; i<79; i++) 
	{
		gotoxy(i,50); printf("%c",219);
		gotoxy(i,0); printf("%c",219);
	}
	for (int i=0; i<50; i++) 
	{
		gotoxy(79,i); printf("%c",219);
		gotoxy(0,i); printf("%c",219);
	}}
}
main()
{
//	loading();
	char ch;
	do{
	ch='0';
	unsigned char c=72;
	int x=10,y=10;
	int Huong = 3;
	int dx[100], dy[100];
	int d=3;
	dx[0]=10; dy[0]=10;
	int tax, tay;
	bool check[100][100];
	bool end=false;
	int old=1;
	for( int i=0; i<100; i++)
	for (int j=0; j<100; j++) check[i][j]=false;
	srand(time(NULL));
	star(ch, check);
	if (ch=='4') huongdan();
	if (ch!='3' || ch!='4')
	{
	
	int diem=0;
	gotoxy(51,0); printf(" DIEM: %d ", diem);
	gotoxy(1,0); printf(" 1.MENU ");
	do
			{
			tax=rand()%78+1; tay=rand()%49+1;	
			}
			while (check[tay][tay]);
			gotoxy(tax,tay); 
			printf("%c",3);                                 //xu sua thanh trai tim
	for (int i=0; i<d; i++) 
	{
		dx[i]=10; dy[i]=10;
	}
	int vt=100;
	do
	{
		Sleep(vt);
		old=Huong;
		if (kbhit())
		{
			c = getch();
			if (c==224)
			{
				c = getch();
				if (c==72) Huong=1;//y=y-1;
				if (c==75) Huong=0;//x=x-1;
				if (c==77) Huong=3;//x=x+1;
				if (c==80) Huong=2;//y=y+1;
			}
		}
		if( Huong+old==3) Huong=old;
		gotoxy(dx[0],dy[0]);
		printf(" ");
		check[dx[0]][dy[0]]=false;
		if (Huong==1) y=y-1;
		if (Huong==0) x=x-1;
		if (Huong==3) x=x+1;
		if (Huong==2) y=y+1;
		if (ch=='2')
		{
			x=x%78; y=y%49;
			if (x==0) x=78;
			if (y==0) y=49;
		}
	
		if (check[x][y] || x<1 || y<1 || x> 78 || y> 49) 
		{
			end=true;
			for(int i=1; i<5; i++)
			{
			
				gotoxy(x,y);
				printf("%c",2);                  //Xu sua
				Sleep(300);
				gotoxy(x,y);
				printf("*");
				Sleep(300);
			}
		}
		if (c=='1') end =true;
		gotoxy(x,y);
		printf("%c",2);                    //xu sua
		check[x][y]=true;
		if(x==tax && y==tay)
		{
			d++;
			if (vt >50) vt=vt-10;
			diem=diem+100;
			gotoxy(57,0);
			printf("%d", diem);
			do
			{
			tax=rand()%78+1; tay=rand()%49+1;	
			}
			while (check[tax][tay]);
			gotoxy(tax,tay); 
			printf("%c",3);          //xu sua
		}
		else
		for(int i=0; i<d; i++)
		{
			dx[i]=dx[i+1];
			dy[i]=dy[i+1];
		}
		dx[d]=x; dy[d]=y;
	}
	while (!end);
	if (c!='1')gameover();
	}}
	while (ch!='3'|| ch!='4');
	
}

